﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using Message = Vito.Csharp.Wpf.Controls;
using Microsoft.Win32;
using System.Text.RegularExpressions;

namespace SweetWeather
{
    public class ConfigManager
    {
        /// <summary>
        /// 根据节点名，获取节点文本内容
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="ElementName">节点名</param>
        /// <returns>返回值，没有返回Null</returns>
        public static string GetElementInnerText(string path, string ElementName)
        {
            XmlDocument doc = new XmlDocument();
            if (File.Exists(path))
            {
                doc.Load(path);
                XmlNode fatherNode = doc.SelectSingleNode("Config");
                XmlNodeList root = fatherNode.ChildNodes;
                foreach (XmlNode item in root)
                {
                    if (item.Name == ElementName)
                        return item.InnerText;
                }
            }
            else
            {
                Message.MessageBox.Show("配置文件不存在，请重新安装", "操作提示");
            }
            return null;
        }

        /// <summary>
        /// 根据节点名，获取节点文本内容
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="ElementName">节点名</param>
        /// <returns>返回值，没有返回Null</returns>
        public static string GetElementInnerText(XmlNode FatherNode, string ElementName)
        {
            XmlNodeList root = FatherNode.ChildNodes;
            foreach (XmlNode item in root)
            {
                if (item.Name == ElementName)
                    return item.InnerText;
            }
            return null;
        }

        /// <summary>
        /// 根据节点名，替换节点文本内容
        /// </summary>
        /// <param name="path"></param>
        /// <param name="ElementName"></param>
        /// <param name="InnerText"></param>
        /// <returns></returns>
        public static bool ReplaceElementInnerText(string path, string ElementName, string InnerText)
        {
            XmlDocument doc = new XmlDocument();
            if (File.Exists(path))
            {
                doc.Load(path);
                XmlNode fatherNode = doc.SelectSingleNode("Config");
                XmlNodeList root = fatherNode.ChildNodes;
                foreach (XmlNode item in root)
                {
                    if (item.Name == ElementName)
                    {
                        item.InnerText = InnerText;
                        doc.Save(path);
                        return true;
                    }
                }
            }
            else
            {
                Message.MessageBox.Show("配置文件不存在，请重新安装", "操作提示");
            }
            return false;
        }

        /// <summary>
        /// 调用浏览器打开链接
        /// </summary>
        /// <param name="str"></param>
        public static void OpenWebUrl(string str)
        {
            RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"http\shell\open\command\");
            string s = key.GetValue("").ToString();

            Regex reg = new Regex("\"([^\"]+)\"");
            MatchCollection matchs = reg.Matches(s);

            string filename = "";
            if (matchs.Count > 0)
            {
                filename = matchs[0].Groups[1].Value;
                System.Diagnostics.Process.Start(filename, str);
            }
        }
    }
}
